######################
#  Replication code for 'Mediating the Electoral Connection', forthcoming in the JOP
#  John Henderson and John Brooks
#  12/7/2015    
######################    

# table1.R
#  :: runs placebo results for table 1 (and also table IV), and additional placebo results w/o controls

# Notes: The main results that are interpreted are found in placebo_sstage1_fe_sumcl, placebo_sstage2_fe_sumcl,
#  placebo_fstage1_fe_sumcl, and placebo_fstage2_fe_sumcl; additional results are interpreted in 
#  placebo_sstage1_sum, placebo_sstage2_sum, placebo_fstage1_sum, and placebo_fstage2_sum

rm(list=ls())
setwd('~/Dropbox/rainReplication')

# loop over data and fixed effects specifications 
for(fes.type in 2:3){ 
non.missings = 4

source('prelimMain.R')   

#detach(covs)
# ANALYSIS

# first stage; rain on prior elections
placebo_fstage1=lm(dose_prv~rain_day,subset=full,data=covs)   
placebo_fstage2=lm(dose_prv~rain_weekend,subset=full,data=covs)
                              
placebo_fstage1_fe=lm(dose_prv~as.factor(fe_id_num)+d_inc+
	dist_prev + midterm + pres_party + black + construction + educ + 
	minc + farmer + forborn + gvtwkr + manuf + pop + unempld + 
	urban + retail + sos + gov + comp_cq + redistricted +rain_day,subset=full,data=covs)  	       
placebo_fstage2_fe=lm(dose_prv~as.factor(fe_id_num)+d_inc+
	dist_prev + midterm + pres_party + black + construction + educ + 
	minc + farmer + forborn + gvtwkr + manuf + pop + unempld + 
	urban + retail + sos + gov + comp_cq + redistricted +rain_weekend,subset=full,data=covs)                                                         

# second stage; rain on prior roll call positions
placebo_sstage1=lm(vote_prv~rain_day,subset=full,data=covs)        
placebo_sstage2=lm(vote_prv~rain_weekend,subset=full,data=covs)

placebo_sstage1_fe=lm(vote_prv~as.factor(fe_id_num)+d_inc+
	dist_prev + midterm + pres_party + black + construction + educ + 
	minc + farmer + forborn + gvtwkr + manuf + pop + unempld + 
	urban + retail + sos + gov + comp_cq + redistricted +rain_day,subset=full,data=covs)         
placebo_sstage2_fe=lm(vote_prv~as.factor(fe_id_num)+d_inc+
 	dist_prev + midterm + pres_party + black + construction + educ + 
	minc + farmer + forborn + gvtwkr + manuf + pop + unempld + 
	urban + retail + sos + gov + comp_cq + redistricted + rain_weekend,subset=full,data=covs)                                                         
     	
    
placebo_fstage1_sum=summary(placebo_fstage1)
placebo_fstage2_sum=summary(placebo_fstage2)

placebo_fstage1_fe_sum=summary(placebo_fstage1_fe)
placebo_fstage2_fe_sum=summary(placebo_fstage2_fe)
            
placebo_fstage1_fe_sumcl=coeftest.cluster(covs[full,],placebo_fstage1_fe,cluster1='as.factor(fe_id_num)')
placebo_fstage2_fe_sumcl=coeftest.cluster(covs[full,],placebo_fstage2_fe,cluster1='as.factor(fe_id_num)')

placebo_sstage1_sum=summary(placebo_sstage1)
placebo_sstage2_sum=summary(placebo_sstage2)
                                           
placebo_sstage1_fe_sum=summary(placebo_sstage1_fe)
placebo_sstage2_fe_sum=summary(placebo_sstage2_fe)
  
placebo_sstage1_fe_sumcl=coeftest.cluster(covs[full,],placebo_sstage1_fe,cluster1='as.factor(fe_id_num)')
placebo_sstage2_fe_sumcl=coeftest.cluster(covs[full,],placebo_sstage2_fe,cluster1='as.factor(fe_id_num)')

save(
placebo_fstage1,
placebo_fstage2,
placebo_fstage1_sum,
placebo_fstage2_sum,
placebo_fstage1_fe,
placebo_fstage2_fe,
placebo_fstage1_fe_sum,
placebo_fstage2_fe_sum,
placebo_fstage1_fe_sumcl,
placebo_fstage2_fe_sumcl,
placebo_sstage1,
placebo_sstage2,
placebo_sstage1_sum,
placebo_sstage2_sum,
placebo_sstage1_fe,
placebo_sstage2_fe,
placebo_sstage1_fe_sum,
placebo_sstage2_fe_sum,
placebo_sstage1_fe_sumcl,
placebo_sstage2_fe_sumcl,

file=paste('main/table1-',non.missings,'_',fes.type,'.Rdata',sep=''))  

rm(
placebo_fstage1,
placebo_fstage2,
placebo_fstage1_sum,
placebo_fstage2_sum,
placebo_fstage1_fe,
placebo_fstage2_fe,
placebo_fstage1_fe_sum,
placebo_fstage2_fe_sum,
placebo_fstage1_fe_sumcl,
placebo_fstage2_fe_sumcl,
placebo_sstage1,
placebo_sstage2,
placebo_sstage1_sum,
placebo_sstage2_sum,
placebo_sstage1_fe,
placebo_sstage2_fe,
placebo_sstage1_fe_sum,
placebo_sstage2_fe_sum,
placebo_sstage1_fe_sumcl,
placebo_sstage2_fe_sumcl
)

}      

# END